package Thread;

import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: homechen
 * Date: 2024-03-15
 * Time: 19:36
 */
public class AtomicDemo2 {
    public static void main(String[] args) {
        AtomicStampedReference<Integer> atomicStampedRef = new AtomicStampedReference<>(100, 0);
        // 获取版本号和值
        int currentStamp = atomicStampedRef.getStamp();
        int currentValue = atomicStampedRef.getReference();

        System.out.println("当前值：" + currentValue + " | 版本号：" + currentStamp);

        int newStamp = currentStamp + 1;
        boolean success = atomicStampedRef.compareAndSet(currentValue,
                200, currentStamp, newStamp);// 从 100 -》 200 版本号 从 current -》 newStamp
        System.out.println("更新结果：" + success);

        int updateValue = atomicStampedRef.getReference();
        int updateStamp = atomicStampedRef.getStamp();

        System.out.println("更新值：" + updateValue + " | 更新版本号：" + updateStamp);
    }
}
